package com.cmccgz.udacs_landlord_webapp.jpush;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.AccreditAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.PushRequestBean;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.jpush.contract.AccreditListContract;
import com.cmccgz.udacs_landlord_webapp.jpush.presenter.AccreditListPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class AccreditListActivity extends BaseActivity implements View.OnClickListener, XRecyclerView.LoadingListener, AccreditListContract.View {
    public static final String KEY_CACHE_DATAS = "key_cache_datas";
    public static final String KEY_NO_ACCREDITED = "key_has_used";
    private List<PushRequestBean> datas = new ArrayList<>();
    private XRecyclerView rv_accredit;
    private LinearLayoutManager layoutManager;
    private AccreditAdapter adapter;
    private AccreditListContract.Presenter presenter;
    private int page = 1;
    private boolean isRequesting = false;
    private static final int REQ_ACCREDIT_DETAIL = 101;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_accredit_list;
    }

    @Override
    protected void initView() {
        ViewHelper instance = ViewHelper.instance(this);
        instance.setBackTextVisable(true);
        instance.setTitle("授权记录");

        rv_accredit = ((XRecyclerView) findViewById(R.id.rv_accredit));

        adapter = new AccreditAdapter(mContext, datas, new OnRvItemClickListenter<PushRequestBean>() {
            @Override
            public void onItemClick(PushRequestBean pushRequestBean, int position, View itemView) {
                Intent intent = new Intent(mContext, AccreditActivity.class);
                intent.putExtra("requestId", pushRequestBean.request_id);
                startActivityForResult(intent, REQ_ACCREDIT_DETAIL);
            }
        });

        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rv_accredit.setLayoutManager(layoutManager);
        rv_accredit.setLoadingListener(this);
        rv_accredit.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_accredit.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_accredit.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        rv_accredit.setAdapter(adapter);

        initDatas();

        presenter = new AccreditListPresenter(this);

        initList();
    }

    private void initDatas() {
        List<PushRequestBean> cacheDatas = (List<PushRequestBean>) getIntent().getSerializableExtra(KEY_CACHE_DATAS);
        datas.clear();
        if (cacheDatas != null) {
            datas.addAll(cacheDatas);
        }
        adapter.notifyDataSetChanged();
        layoutManager.scrollToPosition(0);
    }

    private void initList() {
        showLoadingDialog("正在加载...");
        onRefresh();
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onRefresh() {
        if (isRequesting) {
            showToast("请勿重复操作！");
            return;
        }
        isRequesting = true;
        page = 1;
        presenter.getPushRequestList("", "", page);
    }

    @Override
    public void onLoadMore() {
        if (isRequesting) {
            showToast("请勿重复操作！");
            return;
        }
        isRequesting = true;
        page++;
        presenter.getPushRequestList("", "", page);
    }


    public void stopAll() {
        dismissLoadingDialog();
        rv_accredit.refreshComplete();
        rv_accredit.loadMoreComplete();
        isRequesting = false;
    }

    private void setResult() {
        boolean isNoAccredited = false;
        for (int i = 0; i < datas.size(); i++) {
            if (isNoAccredited = !datas.get(i).has_use)
                break;
        }
        Intent data = new Intent();
        data.putExtra(KEY_NO_ACCREDITED, isNoAccredited);
        setResult(RESULT_OK, data);
    }

    @Override
    public void refreshPushReqListSuccess(List<PushRequestBean> datas) {
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
        isRequesting = false;
        adapter.notifyDataSetChanged();
        layoutManager.scrollToPosition(0);
        stopAll();
    }

    @Override
    public void loadPushReqListSuccess(List<PushRequestBean> datas) {
        if (datas != null) {
            this.datas.addAll(datas);
        }
        isRequesting = false;
        adapter.notifyDataSetChanged();
        stopAll();
    }

    @Override
    public void refreshError(String msg) {
        stopAll();
    }

    @Override
    public void loadError(String msg) {
        page--;
        stopAll();
    }

    @Override
    public void onBackPressed() {
        setResult();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_ACCREDIT_DETAIL && resultCode == RESULT_OK)
            initList();
    }
}
